<template>
  <!--
    	作者：luoyiming
    	时间：2019-10-24
    	描述：商城管理
    -->
  <div v-loading="loading">
    <!--添加商城-->
    <div class="common-level-rail">
      <el-button size="small" type="primary" @click="addClick"
        >添加商城</el-button
      >
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <div>
          <el-table
            size="small"
            :data="tableData"
            style="width: 100%"
            border
            default-expand-all
            v-loading="loading"
          >
            <el-table-column prop="appId" label="商城ID"></el-table-column>
            <el-table-column prop="appName" label="商城名稱"></el-table-column>
            <el-table-column prop="userName" label="超管賬號"></el-table-column>
            <el-table-column prop="isRecycle" label="狀態">
              <template #default="scope">
                <el-checkbox
                  v-model="scope.row.isRecycle"
                  @change="(checked) => statusChange(checked, scope.row)"
                  >啓用</el-checkbox
                >
              </template>
            </el-table-column>
            <el-table-column prop="weixin_service" label="微信服務商支付">
              <template #default="scope">
                <el-checkbox
                  v-model="scope.row.weixinService"
                  @change="(checked) => wxStatusChange(checked, scope.row)"
                  >啓用</el-checkbox
                >
              </template>
            </el-table-column>
            <el-table-column prop="expireTime" label="過期時間">
              <template #default="scope">
                <span v-if="scope.row.expireTime == null">永不過期</span>
                <span v-if="scope.row.expireTime != null">{{
                  scope.row.expireTime
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="添加時間"
            ></el-table-column>
            <el-table-column label="操作" width="150">
              <template #default="scope">
                <!-- <el-link
                  :href="'/shop/'"
                  target="_blank"
                  link
                  type="primary"
                  size="small"
                  >進入商城</el-link
                > -->
                <el-link
                  class="ml10"
                  @click="editClick(scope.row)"
                  link
                  type="primary"
                  size="small"
                  >編輯</el-link
                >
                <el-link
                  class="ml10"
                  @click="deleteClick(scope.row)"
                  link
                  type="primary"
                  size="small"
                  >刪除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <!--添加-->
    <Add
      v-if="open_add"
      :open_add="open_add"
      @closeDialog="closeDialogFunc($event, 'add')"
    ></Add>

    <!--编辑-->
    <Edit
      v-if="open_edit"
      :open_edit="open_edit"
      :curModel="curModel"
      @closeDialog="closeDialogFunc($event, 'edit')"
    ></Edit>

    <!--分页-->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import ShopApi from "@/api/shop.js";
import Edit from "./part/Edit.vue";
import Add from "./part/Add.vue";
import { deepClone } from "@/utils/base.js";
export default {
  components: {
    /*编辑组件*/
    Edit: Edit,
    Add: Add
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表格数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      curModel: {}
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      ShopApi.shopList({
        pageIndex: self.curPage,
        pageSize: self.pageSize
      }, true).then(res => {
        self.loading = false;
        self.tableData = res.data.records;
        self.totalDataNumber = res.data.total;
        self.tableData.forEach(function (message) {
          message.isRecycle = !message.isRecycle;
        });
      }).catch(error => {});
    },
    /*打开添加*/
    addClick() {
      this.open_add = true;
    },
    /*打开编辑*/
    editClick(e) {
      this.open_edit = true;
      this.curModel = deepClone(e);
      if (this.curModel.expireTime == null) {
        this.curModel.expireTime = null;
        this.curModel["noExpire"] = true;
      } else {
        this.curModel["noExpire"] = false;
      }
    },
    /*关闭*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    },
    /*启用*/
    statusChange: function (checked, row) {
      let self = this;
      self.loading = true;
      ShopApi.updateStatus({
        appId: row.appId
      }, true).then(data => {
        self.loading = false;
        row.isRecycle = checked;
      }).catch(error => {
        self.loading = false;
        row.isRecycle = !checked;
      });
    },
    /*启用*/
    wxStatusChange: function (checked, row) {
      let self = this;
      self.loading = true;
      ShopApi.updateWxStatus({
        appId: row.appId
      }, true).then(data => {
        self.loading = false;
        row.weixinService = checked;
      }).catch(error => {
        self.loading = false;
        row.weixinService = !checked;
      });
    },
    /*删除商城*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm("刪除後不可恢復，確認刪除該記錄嗎?", "提示", {
        confirmButtonText: "確定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        self.loading = true;
        ShopApi.deleteShop({
          appId: row.appId
        }, true).then(data => {
          self.loading = false;
          if (data.code == 1) {
            ElMessage({
              message: data.msg,
              type: "success"
            });
            self.getData();
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    },
    /*进入商城*/
    storeEnter(e) {
      let self = this;
      ShopApi.storeEnter({
        appId: e
      }, true).then(data => {}).catch(error => {});
    }
  }
};
</script>

<style>
.el-link.is-underline:hover {
  opacity: 0.8;
}
.el-link.is-underline:hover:after {
  display: none;
}
</style>

